package com.scujj.cs6.controller;

import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.dto.comment.CommentDTO;
import com.scujj.cs6.model.dto.comment.CommentUpdateDTO;
import com.scujj.cs6.model.vo.CommentVo;
import com.scujj.cs6.model.vo.MyCommentVo;
import com.scujj.cs6.model.vo.PageVo;
import com.scujj.cs6.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "评论接口", description = "评论相关 API")
@RestController
@RequestMapping("/comments")
@Validated
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Operation(summary = "分页显示回答的所有评论")
    @GetMapping("/{id}")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = CommentVo.class))
            )
    )
    public Result<PageVo<CommentVo>> pageComments(@Parameter(name = "id" , description = "回答ID", required = true , in = ParameterIn.PATH) @PathVariable Long id ,
                                                  @Parameter(name = "page",description = "页码")
                                                  @RequestParam(defaultValue = "1" , required = false) int page ,
                                                  @Parameter(name = "size",description = "每页数量") @RequestParam(defaultValue = "10" , required = false)int size) {
        return Result.success(commentService.pageComments(id,page,size));
    }
    //显示我评论过的问题
    @Operation(summary = "分页显示我的评论")
    @GetMapping("/my")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = MyCommentVo.class))
            )
    )
    public Result<PageVo<MyCommentVo>> pageMyComments(@Parameter(name = "page",description = "页码")
                                                        @RequestParam(defaultValue = "1" , required = false) int page ,
                                                      @Parameter(name = "size",description = "每页数量")
                                                    @RequestParam(defaultValue = "10" , required = false) int size , HttpServletRequest request) {
        return Result.success(commentService.pageMyComments(page,size,request));
    }


    @Operation(summary = "创建评论", description = "用户登录后可创建评论")
    @PostMapping("/")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> createComment(@Valid @RequestBody CommentDTO commentDTO , HttpServletRequest request) {
        commentService.createComment(commentDTO,request);
        return Result.success();
    }

    @Operation(summary = "删除评论", description = "用户登录后可删除评论")
    @DeleteMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> deleteComment(@Parameter(name = "id" , description = "评论ID", required = true , in = ParameterIn.PATH) @PathVariable Long id , HttpServletRequest request) {
        commentService.deleteComment(id,request);
        return Result.success();
    }

    @Operation(summary = "更新评论", description = "用户登录后可更新评论")
    @PutMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> updateComment(@Parameter(name = "id" , description = "评论ID", required = true , in = ParameterIn.PATH) @PathVariable Long id , @Valid @RequestBody CommentUpdateDTO commentDTO , HttpServletRequest request) {
        commentService.updateComment(id,commentDTO,request);
        return Result.success();
    }
}